using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLThuChi.Repositories
{
	#region using
	using QLThuChi.Models;
	#endregion
	public class CongTrinh_GiaiDoanRepository
	{
		public static List<CongTrinh_GiaiDoan> SelectAll()
		{
			return DataContext.Instance.CongTrinh_GiaiDoan.ToList();
		}

		public static CongTrinh_GiaiDoan SelectByID(int macongtrinhgiaidoan)
		{
			return DataContext.Instance.CongTrinh_GiaiDoan.FirstOrDefault(item => item.MaCongTrinhGiaiDoan == macongtrinhgiaidoan );
		}

        public static CongTrinh_GiaiDoan SelectByCongTrinhGiaiDoan(int macongtrinh, int magiaidoan)
        {
            return DataContext.Instance.CongTrinh_GiaiDoan.FirstOrDefault(item => item.MaCongTrinh == macongtrinh && item.MaGiaiDoan == magiaidoan);
        }

		public static bool Insert(CongTrinh_GiaiDoan obj)
		{
			try
			{
				DataContext.Instance.CongTrinh_GiaiDoan.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static bool Delete(int macongtrinhgiaidoan)
		{
			try
			{
				var delitem = DataContext.Instance.CongTrinh_GiaiDoan.FirstOrDefault(item => item.MaCongTrinhGiaiDoan == macongtrinhgiaidoan );
				DataContext.Instance.CongTrinh_GiaiDoan.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

        public static bool DeleteByMaCongTrinh(int macongtrinh)
        {
            try
            {
                var lstItem = CongTrinh_GiaiDoanRepository.SelectByMaCongTrinh(macongtrinh);
                for (int i = 0; i < lstItem.Count; i++ )
                {
                    if (!Delete(lstItem[i].MaCongTrinhGiaiDoan))
                    {
                        return false;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public static List<CongTrinh_GiaiDoan> RetrieveByID(int macongtrinhgiaidoan)
		{
			return (from item in DataContext.Instance.CongTrinh_GiaiDoan where  item.MaCongTrinhGiaiDoan == macongtrinhgiaidoan  select item).ToList();
		}

		public static List<CongTrinh_GiaiDoan> SelectByMaCongTrinh(int macongtrinh)
		{
			var lstItem = (from item in DataContext.Instance.CongTrinh_GiaiDoan where item.MaCongTrinh == macongtrinh select item).ToList();
			return lstItem;
		}

		public static List<CongTrinh_GiaiDoan> SelectByMaGiaiDoan(int magiaidoan)
		{
			var lstItem = (from item in DataContext.Instance.CongTrinh_GiaiDoan where item.MaGiaiDoan == magiaidoan select item).ToList();
			return lstItem;
		}

	}
}